package com.fhz.a2102shopjava.ui.home;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.fhz.a2102shopjava.R;
import com.fhz.a2102shopjava.adapter.ImageAdapter;
import com.fhz.a2102shopjava.base.BaseFragment;
import com.fhz.a2102shopjava.callback.ApiResponse;
import com.fhz.a2102shopjava.databinding.FragmentHomeBinding;
import com.fhz.a2102shopjava.entity.Banner;
import com.fhz.a2102shopjava.entity.GoodsInfo;
import com.fhz.a2102shopjava.entity.ResponseBean;
import com.fhz.a2102shopjava.ui.details.DetailsActivity;
import com.fhz.a2102shopjava.ui.goods.GoodsViewModel;
import com.fhz.a2102shopjava.util.ImageLoader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.youth.banner.adapter.BannerAdapter;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import dagger.hilt.android.AndroidEntryPoint;


@AndroidEntryPoint
public class HomeFragment extends BaseFragment<HomeViewModel, FragmentHomeBinding> {

    private static final String TAG = "HomeFragment";
    HomeGoodsRecyclerViewAdapter homeGoodsRecyclerViewAdapter;

    ArrayList<GoodsInfo> goodsInfo;
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initData() {
        setBanner();
        setRecyclerView();
    }

    /**
     * 设置适配器
     */
    private void setRecyclerView() {
        mBinding.homeShopRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
        goodsInfo = new ArrayList<>();
        homeGoodsRecyclerViewAdapter = new HomeGoodsRecyclerViewAdapter(getActivity(),goodsInfo);
        mBinding.homeShopRecyclerView.setAdapter(homeGoodsRecyclerViewAdapter);

        mBinding.mainRefreshLayout.setOnRefreshListener(refreshLayout -> {
            //去请求新的数据
//                handler.sendEmptyMessage(200)
            refreshLayout.finishRefresh();
        });
        mBinding.mainRefreshLayout.setOnLoadMoreListener( refreshlayout ->{
                //加载更多数据
                refreshlayout.finishLoadMore(2000 /*,false*/); //传入false表示加载失败
        });
        mViewModel.getGoodsInfo(1,10).observe(this, new Observer<ApiResponse<ResponseBean<List<GoodsInfo>>>>() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onChanged(ApiResponse<ResponseBean<List<GoodsInfo>>> responseBeanApiResponse) {
                goodsInfo.addAll(responseBeanApiResponse.getData().getList());
                homeGoodsRecyclerViewAdapter.notifyDataSetChanged();
            }
        });

        homeGoodsRecyclerViewAdapter.setItemClickListener(new HomeGoodsRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int postion) {
                DetailsActivity.launch(getActivity(),goodsInfo.get(postion));
            }
        });

    }

    /**
     * 设置banner
     */
    private void setBanner() {
        mBinding.banner.addBannerLifecycleObserver(this);
        mBinding.banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(Object data, int position) {
                Log.i(TAG, "OnBannerClick: ");
                Toast.makeText(getActivity(), "先别点了"+position, Toast.LENGTH_SHORT).show();
            }
        });

        //更新数据源
        mViewModel.getBanner(0,1,5).observe(this, new Observer<ApiResponse<ResponseBean<List<Banner>>>>() {
            @Override
            public void onChanged(ApiResponse<ResponseBean<List<Banner>>> responseBeanApiResponse) {
                System.out.println(responseBeanApiResponse);
                mBinding.banner.setAdapter(new BannerImageAdapter<Banner>(responseBeanApiResponse.getData().getList()) {
                            @Override
                            public void onBindView(BannerImageHolder holder, Banner data, int position, int size) {
                                ImageLoader.load(holder.itemView.getContext(),data.getImagepath(),holder.imageView);
                            }
                        })
                        .setIndicator(new CircleIndicator(getActivity()));
            }
        });
    }

    @Override
    protected HomeViewModel createViewModel() {
        return new ViewModelProvider(this).get(HomeViewModel.class);
    }
}